package com.dao;

import com.bean.Product;
import com.db.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImp implements ProductDao{

    public List<Product> findAllProductByName(String productName){
        String sql = "select name,price from product where name like \"%"+productName+"%\"";
        Connection con = DBUtils.getConn();
        PreparedStatement ps=null;
        List<Product> list = null;
        try {
            ps=con.prepareStatement(sql);
            list=new ArrayList<>();
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                Product product=new Product();
                String name = rs.getString("name");
                product.setName(name);
                list.add(product);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
